package br.com.grunfeldt.testelist;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListStringsAdapter extends BaseAdapter{

	private List<Item> listString;
	private LayoutInflater inflater;
	
	public ListStringsAdapter(Activity context, List<Item> listString) {
		this.listString = listString;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return listString.size();
	}

	@Override
	public Object getItem(int position) {
		return listString.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		
		ViewHolder holder;
		
		if(view==null){
			view = inflater.inflate(R.layout.layout_view, null);
						
			holder = new ViewHolder();
			holder.mensagemBold = (TextView) view.findViewById(R.id.mensagemBold);
			holder.mensagemNormal = (TextView) view.findViewById(R.id.mensagemNormal);
			holder.mensagemLateral = (TextView) view.findViewById(R.id.mensagemLateral);
			holder.imagem = (ImageView) view.findViewById(R.id.imagem);
			
			view.setTag(holder);
		}else{		
			holder = (ViewHolder) view.getTag();
		}
		
		Item item = listString.get(position);
		holder.mensagemBold.setText(item.mensagemBold);
		holder.mensagemLateral.setText(item.mensagemLateral);
		if(item.mensagemNormal!=null && item.mensagemNormal.length()>25){
			holder.mensagemNormal.setText(item.mensagemNormal.subSequence(0, 24) + "...");
		}else{		
			holder.mensagemNormal.setText(item.mensagemNormal);
		}
		
		holder.imagem.setImageResource(item.imagem);
		
		return view;
		
	}

	static class ViewHolder{
		TextView mensagemBold;
		TextView mensagemNormal;
		TextView mensagemLateral;
		ImageView imagem;
	}

}
